//#include <head.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <signal.h>

#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/un.h>

#define DEFAULT_PORT (9000)

#define SERVER_SOCKET "server.socket"
#define CLIENT_SOCKET "client.socket"
#define CLEAR_ARRAY(a) memset(a,0,sizeof(a))
#define CLEAR_MEM(addr,len) memset(addr,0,len)
static int handle_socket_read(int fd, void *buf, size_t count);
static int handle_socket_write(int fd, const void *buf, size_t count);

static void init_signal_process(void);

void fn(char *p);
int main(int argc ,char** argv)
{
    int r=0;
    uint16_t port =DEFAULT_PORT;
    char buf[BUFSIZ];
    int fd =-1;
    struct sockaddr_in  addr ;
    socklen_t len;
   
    if(argc ==2){
        port =atoi(argv[1]);
    }
    init_signal_process();
  
retry:
    fd = socket(AF_INET,SOCK_STREAM,0);
    if(fd<0){
        printf("socket err:%s\n",strerror(errno));
        return -1;
    }
    memset(&addr,0,sizeof(addr));
    addr.sin_family =AF_INET;
    addr.sin_port = htons((port));
    r= inet_pton(AF_INET,"127.0.0.1",&(addr.sin_addr.s_addr));
    if(r ==0){
        perror("inet_pton");
    }    
    len =sizeof(addr);

    while(1){
        r= connect(fd,(struct sockaddr*)&addr,len);
        if(r< 0){
            printf("connect err:%s\n",strerror(errno));
            sleep(1);
            close(fd);
            goto retry;

        }else if(r>0){
            printf("connect server ok!\n");
        }
        while(1){
             CLEAR_ARRAY(buf);
            //r= read(STDIN_FILENO,buf,sizeof(buf));
            //handle_socket_write(fd,buf,r);


            r= handle_socket_read(fd,buf,sizeof(buf));
            if(r== 0){
                printf("server close!\n");
                close(fd);
                sleep(1);
                goto retry;
            }else if(r<0){
                printf("read err:%s\n",strerror(errno));

            }else if(r>0){
                write(STDOUT_FILENO,buf,r);
            }
        }
       
    }
    
    close(fd);
    return 0;
}
void fn(char *p)
{
    if(!p){
        return;
    }
    while (*p)
    {
        *p =toupper(*p);
        p++;
    }
    
}
static int handle_socket_write(int fd, const void *buf, size_t count)
{
    ssize_t r = write(fd, buf,  count);
    if(r== 0){
        printf("write 0 byte!!\n");
        close(fd);
    }else if(r<0){
        printf("write err:%s\n",strerror(errno));
    }else if(r>0){
        //write(STDOUT_FILENO,buf,r);
        printf("write %ld bytes.\n",r);
    }
    return r;
}
static int handle_socket_read(int fd, void *buf, size_t count)
{
    ssize_t r = read(fd, buf,  count);
    if(r== 0){
        printf("server close!\n");
        close(fd);
    }else if(r<0){
        printf("read err:%s\n",strerror(errno));
    }else if(r>0){
        printf("read %ld  bytes.\n",r);
    }
    return r;
}
static void sig_fun(int sig)
{
    switch(sig){
    case SIGINT:
    case SIGTERM:	// kill -15 pid			
    case SIGSEGV:
    case SIGPIPE:
    //case SIGKILL:   // kill -9 pid   ϵͳĬ����Ϊ, ���ܸ���
       
        
        // sigprocess
        printf("--- sig %d\n",sig);
        //system_uninit();
        //_exit(1); //exit(1)
        break;
    //*	
    case SIGIO: 
        
        break;
    //*/	
    default:
        printf("--- sigX %d\n",sig);
        break;
    }
}
static void init_signal_process(void)
{
    signal(SIGPIPE,sig_fun);
}

